import axios from 'axios'

const axiosInstance = axios.create({
  baseURL: '',
  timeout: 5000,
  headers: {}
})

//请求拦截器
axiosInstance.interceptors.request.use((config: any) => {
  config._time = Date.now()
  return config
}, (error) => {
  return Promise.reject(error)
});

//响应拦截器
axiosInstance.interceptors.response.use((response: any) => {
  const data = response.data, config = response.config
  const time = Date.now()
  const duration = time - config._time
  if (data.IsSuccess) {
    return Promise.resolve(data.data)
  } else {
    __ml.api(config.url, false, duration, data.IsSuccess, data.Data)
    return Promise.reject(data.msg)
  }
}, (error) => {
  const time = Date.now()
  const duration = time - error.config._time
  __ml.api(error.config.url, false, duration, error.response.status, error.message)
  return Promise.reject(error)
})

export default axiosInstance